<template>
  <div>
    <el-row :gutter="40">
      <el-col :span="8">
        <el-upload
          class="upload-demo"
          ref="upload"
          action="action"
          multiple
          :limit="1"
          :before-upload="uploadNewExcel"
        >
          <el-button type="primary" slot="trigger" :loading="loading"
            >上传图片</el-button
          >
        </el-upload>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "ExcelDemo",
  data() {
    return {
      loading: false,
      towerCode: "bf5e6deb12e9b684a5c28ab045999dd0",
    };
  },
  created() {},
  methods: {
    uploadNewExcel(file) {
      const extension = file.name.split(".")[1] === "jpg";
      const extension2 = file.name.split(".")[1] === "png";
      if (!extension && !extension2) {
        this.$message({
          message: "上传只能是 jpg、png格式!",
          type: "error",
        });
        return;
      }

      this.loading = true;
      let that = this;
      const formData = new FormData();
      formData.append("file", file);
      formData.append("TowerCode",that.towerCode);
      this.axios({
        url: ` http://127.0.0.1:81/d/con_tower/imageUpload`,
        method: "post",
        data: formData,
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
        .then((res) => {
          this.loading = false;
          this.$message({
            message: "上传成功!",
            type: "success",
          });
        })
        .catch((err) => {
          console.error(err);
          this.loading = false;
        });
    },
  },
};
</script>
